package edu.xmut.kakamusic.activity.tiktok;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import edu.xmut.kakamusic.R;
import edu.xmut.kakamusic.bean.vo.RecommendVideoVO;

public class TikTokRecyclerViewAdapter extends RecyclerView.Adapter<TikTokRecyclerViewAdapter.MyViewHolder> {

    public static final String TAG = TikTokRecyclerViewAdapter.class.getSimpleName();
    private final ArrayList<RecommendVideoVO> data = new ArrayList<>();


    public void setData(List<RecommendVideoVO> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tiktok, parent, false));
    }

    @SuppressLint({"LongLogTag", "SetTextI18n"})
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        RecommendVideoVO video = data.get(position);
        JZDataSource jzDataSource = new JZDataSource(video.getData().getUrlInfo().getUrl());
        jzDataSource.looping = true;
        holder.jzvdStd.setUp(jzDataSource, Jzvd.SCREEN_NORMAL);
        Glide.with(holder.jzvdStd.getContext())
                .load(video.getData().getCoverUrl())
                .into(holder.jzvdStd.posterImageView);
        // 设置页面信息
        holder.title.setText(video.getData().getTitle());
        holder.creatorNickname.setText("@" + video.getData().getCreator().getNickname());
        if (Objects.nonNull(video.getData().getRelateSong()) && !video.getData().getRelateSong().isEmpty()) {
            holder.musicName.setText(video.getData().getRelateSong().get(0).getName());
        } else {
            holder.musicName.setText(video.getData().getCreator().getSignature());
        }
        holder.likeCount.setText(video.getData().getPraisedCount() + "");
        holder.commentCount.setText(video.getData().getCommentCount() + "");
        holder.shareCount.setText(video.getData().getShareCount() + "");
        Glide.with(holder.avatar.getContext())
                .load(data.get(position).getData().getCreator().getAvatarUrl())
                .into(holder.avatar);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView title;
        private final TextView creatorNickname;
        private final TextView musicName;
        private final ImageView avatar;
        private final TextView likeCount;
        private final TextView commentCount;
        private final TextView shareCount;
        JzvdStdTikTok jzvdStd;

        public MyViewHolder(View itemView) {
            super(itemView);
            jzvdStd = itemView.findViewById(R.id.tiktok_video);
            title = itemView.findViewById(R.id.tv_tiktok_title);
            creatorNickname = itemView.findViewById(R.id.tv_tiktok_creator_nickname);
            musicName = itemView.findViewById(R.id.tv_tiktok_music_name);
            avatar = itemView.findViewById(R.id.iv_tiktok_avatar);
            likeCount = itemView.findViewById(R.id.tv_tiktok_like);
            commentCount = itemView.findViewById(R.id.tv_tiktok_comment);
            shareCount = itemView.findViewById(R.id.tv_tiktok_share);
        }
    }

}
